package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.CustomerTransfer;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.CustomerMapper;
import cn.wolfcode.crm.mapper.CustomerTransferMapper;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.ICustomerTransferService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CustomerTransferServiceimpl implements ICustomerTransferService {

    @Autowired
    private CustomerTransferMapper customerTransferMapper;
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public void save(CustomerTransfer customerTransfer) {
        //设置操作员
        Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
        //新销售员id
        Long newid = null;
        //客户id
        Long customerid = customerTransfer.getCustomer().getId();
        if (customerTransfer.getCustomer().getStatus() == 4){
            newid = employee.getId();
        }else{
            newid = customerTransfer.getNewseller().getId();
        }
        //更新销售人员
        customerMapper.updateBySellerid(customerid,newid);

        customerTransfer.setOperator(employee);
        //设置操作时间
        customerTransfer.setOperateTime(new Date());
        customerTransferMapper.insert(customerTransfer);
    }

    @Override
    public void deleteByPrimaryKey(Long id) {

        customerTransferMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void updateByPrimaryKey(CustomerTransfer customerTransfer) {
        customerTransferMapper.updateByPrimaryKey(customerTransfer);
    }

    @Override
    public CustomerTransfer get(Long id) {
        return customerTransferMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<CustomerTransfer> selectAll() {
        return customerTransferMapper.selectAll();
    }

    @Override
    public PageInfo query(QueryObject qo) {
        /*int count = customerTransferMapper.queryForCount(qo);
        if (count==0) {
            return new PageResult<CustomerTransfer>(qo.getPageSize());
        }*/
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<CustomerTransfer> list = customerTransferMapper.queryForList(qo);
        return new PageInfo(list);
    }
}
